#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.shortcuts import render, HttpResponse, redirect
from web.forms.account import RegisiterModelForm,SendSmsForm
from django.http import JsonResponse
import random
from utils.tencent.sms import send_sms_single
from app01 import models
from django import forms
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError

def register(request,*args,**kwargs):
    '''
    注册
    :param request:
    :param args:
    :param kwargs:
    :return:
    '''
    if request.method == 'GET':
        form = RegisiterModelForm()
        return render(request, 'web/register.html',{'form':form})
    else:
        form = RegisiterModelForm(data=request.POST)
        if form.is_valid():
            print(form.cleaned_data)
        else:
            print(form.errors)

def send_sms(request):
    '''发送短信'''
    mobile_phone = request.GET.get('mobile_phone')
    tpl = request.GET.get('tpl')
    print(mobile_phone,tpl)
    form = SendSmsForm(request,data=request.GET)
    # 只校验手机号，是否为空，是否格式错误
    if form.is_valid():
        return JsonResponse({'status':True})
    return JsonResponse({'status':False,'error':form.errors})

